package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.io.FileInputStream;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map) {
        //获取前台输入的验证码
        String validateCode_input = (String) map.get("validateCode");
        //获取保存在redis中的key
        String telephone = (String) map.get("telephone");
        String key = (String) telephone + RedisMessageConstant.SENDTYPE_ORDER;
        //从redis中获取保存的验证码
        String validateCode_redis = jedisPool.getResource().get(key);
//        if (validateCode_input != "" && validateCode_input != null && validateCode_redis != null && validateCode_input.equals(validateCode_redis)) {
            //如果验证码输入正确
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            //进行预约
            Result result = new Result(false,"预约失败...");
            try {
                result = orderService.order(map);
                //预约成功,为用户发送预约成功短信
//                if (result.isFlag()) {
//                    String orderDate = (String) map.get("orderDate");
//                    try {
//                        SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, orderDate);
//                    } catch (ClientException e) {
//                        e.printStackTrace();
//                    }
//                }
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return result;
            }
//        } else {
//            //验证码输入错误
//            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
//        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map=orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }
}
